package cn.com.haopy.yue.ai.db.service.hos.impl;

import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskPapers;
import cn.com.haopy.yue.ai.db.mapper.arisk.AIRiskPapersMapper;
import cn.com.haopy.yue.ai.db.service.BaseServiceImpl;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskPapersService;
import cn.com.haopy.yue.ai.db.utils.SqlInjectionExtUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2025/3/26 9:13
 */
@Service
public class AIRiskPapersServiceImpl extends BaseServiceImpl<AIRiskPapersMapper, AIRiskPapers> implements AIRiskPapersService {

    @Override
    public AIRiskPapers getById(Integer id) {
        return super.getById(id);
    }

    @Override
    public Page<AIRiskPapers> querySelective(Integer userId, Integer page, Integer limit, String sort, String order) {
        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }

        QueryWrapper<AIRiskPapers> queryWrapper = new QueryWrapper<>();
        //queryWrapper.lambda().eq(AIRiskPapers::getIsSubmit, Boolean.TRUE);
        if (Objects.nonNull(userId)) {
            queryWrapper.lambda().eq(AIRiskPapers::getUserId, userId);
        }
        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return super.page(new Page<>(page, limit), queryWrapper);
    }

    @Override
    public Page<AIRiskPapers> querySelective(String name, String checkNo, String mobile, Integer page, Integer limit, String sort, String order) {
        if (SqlInjectionExtUtils.check(sort) || SqlInjectionExtUtils.check(order)) {
            throw new RuntimeException("存在非法字段");
        }
        QueryWrapper<AIRiskPapers> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(name)) {
            queryWrapper.lambda().like(AIRiskPapers::getName, name);
        }
        if (StringUtils.hasText(checkNo)) {
            queryWrapper.lambda().eq(AIRiskPapers::getCheckNo, checkNo);
        }
        if (StringUtils.hasText(mobile)) {
            queryWrapper.lambda().eq(AIRiskPapers::getMobile, mobile);
        }
        if (StringUtils.hasText(sort) && StringUtils.hasText(order)) {
            queryWrapper.orderBy(true, "asc".equals(order), sort);
        }
        return super.page(new Page<>(page, limit), queryWrapper);
    }

    @Override
    public List<AIRiskPapers> queryCreateWaitPdf() {
        QueryWrapper<AIRiskPapers> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(AIRiskPapers::getIsSubmit, true);

        queryWrapper.lambda().and(wp -> wp
                .isNull(AIRiskPapers::getPdfUrl)
                .or()
                .eq(AIRiskPapers::getPdfUrl, ""));
        return super.list(queryWrapper);
    }
}
